package com.robam.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.robam.gulimall.product.entity.CategoryBrandRelationEntity;
import com.robam.gulimall.product.service.CategoryBrandRelationService;
import com.robam.common.utils.PageUtils;
import com.robam.common.utils.R;



/**
 * 品牌分类关联
 *
 * @author bobo
 * @email 412467850@qq.com
 * @date 2020-10-15 08:42:51
 */
@Api(description = "商品三级分类")
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    /**
     * 列表
     */
    @GetMapping("/list")
    //@RequiresPermissions("product:categorybrandrelation:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryBrandRelationService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    //@RequiresPermissions("product:categorybrandrelation:info")
    public R info(@PathVariable("id") Long id){
		CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);
        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     *  根据品牌id获取此品牌已绑定的三级分类
     * @param brandId 品牌id
     * @return
     */
    @GetMapping("/getRelationByBrandId/{brandId}")
    public R getRelationByBrandId(@PathVariable("brandId") Long brandId) {
        QueryWrapper<CategoryBrandRelationEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("brand_id", brandId);
        List<CategoryBrandRelationEntity> relationEntities = categoryBrandRelationService.list(wrapper);
        return R.ok().put("data", relationEntities);
    }

    @GetMapping("/getRelationByCatgoryId/{catgoryId}")
    public R getBrandsByCatgoryId(@PathVariable("catgoryId") Long catgoryId) {
        QueryWrapper<CategoryBrandRelationEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("catelog_id", catgoryId);
        List<CategoryBrandRelationEntity> relationEntities = categoryBrandRelationService.list(wrapper);
        return R.ok().put("data", relationEntities);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    //@RequiresPermissions("product:categorybrandrelation:save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
        // 校验前端传过来的数据是否合法
        boolean isValidate = categoryBrandRelationService.saveValidate(categoryBrandRelation);
        if(isValidate) {
            categoryBrandRelationService.save(categoryBrandRelation);
        }
        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    //@RequiresPermissions("product:categorybrandrelation:update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.updateById(categoryBrandRelation);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    //@RequiresPermissions("product:categorybrandrelation:delete")
    public R delete(@RequestBody Long[] ids){
		categoryBrandRelationService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

}
